package com.pentalog.ginet;

import java.util.*;

import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.pentalog.ginet.domain.Certificate;
import com.pentalog.ginet.domain.Employee;
import com.pentalog.ginet.domain.PEmployee;

public class ManagePEmployee {
   private static SessionFactory factory; 
   public static void main(String[] args) {
      try{
         factory = new Configuration().configure().buildSessionFactory();
      }catch (Throwable ex) { 
         System.err.println("Failed to create sessionFactory object." + ex);
         throw new ExceptionInInitializerError(ex); 
      }
      ManagePEmployee ME = new ManagePEmployee();
      /* Let us have a set of certificates for the first employee  
      ArrayList<Certificate> set1 = new ArrayList<Certificate>();
      set1.add(new Certificate("MCA"));
      set1.add(new Certificate("MBA"));
      set1.add(new Certificate("PMP"));
     
       Add employee records in the database 
      Integer empID1 = ME.addPEmployee("Manoj", "Kumar", 4000, set1);*/

       //Another set of certificates for the second employee  
      ArrayList<Certificate> set2 = new ArrayList<Certificate>();
      set2.add(new Certificate("CBCA"));
      set2.add(new Certificate("CBA"));

       //Add another employee record in the database 
//      Integer empID2 = ME.addPEmployee("acvc", "dfs", 3000, set2);

      /* List down all the employees */
      ME.listPEmployees();

      /* Update employee's salary records */
//      ME.updatePEmployee(empID1, 5000);

      /* Delete an employee from the database */
//      ME.deletePEmployee(empID2);

      /* List down all the employees */
//      ME.listPEmployees();

   }

   /* Method to add an employee record in the database */
   public Integer addPEmployee(String fname, String lname, 
                                      int salary, ArrayList<Certificate> cert){
      Session session = factory.openSession();
      Transaction tx = null;
      Integer employeeID = null;
      try{
         tx = session.beginTransaction();
         PEmployee employee = new PEmployee(fname, lname, salary);
         employee.setCertificates(cert);
         employeeID = (Integer) session.save(employee); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
      return employeeID;
   }

   /* Method to list all the employees detail */
   public void listPEmployees( ){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         List<PEmployee> employees = session.createQuery("FROM PEmployee").list(); 
         for (Iterator<PEmployee> iterator1 = 
                           employees.iterator(); iterator1.hasNext();){
            PEmployee employee = (PEmployee) iterator1.next(); 
            System.out.print("First Name: " + employee.getFirstName()); 
            System.out.print("  Last Name: " + employee.getLastName()); 
            System.out.println("  Salary: " + employee.getSalary());
            List<Certificate> certificates = employee.getCertificates();
            if(certificates != null){
            	System.out.println("===================OK");
            }
            /*for (Iterator<Certificate> iterator2 = 
                         certificates.iterator(); iterator2.hasNext();){
                  Certificate certName = (Certificate) iterator2.next(); 
                  System.out.println("Certificate: " + certName.getName()); 
            }*/
         }
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }
   /* Method to update salary for an employee */
   public void updatePEmployee(Integer PEmployeeID, int salary ){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         PEmployee employee = 
                    (PEmployee)session.get(PEmployee.class, PEmployeeID); 
         employee.setSalary( salary );
         session.update(employee);
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }
   /* Method to delete an employee from the records */
   public void deletePEmployee(Integer PEmployeeID){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         PEmployee employee = 
                   (PEmployee)session.get(PEmployee.class, PEmployeeID); 
         session.delete(employee); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }
}
